import os

import requests

from django.shortcuts import render, redirect, HttpResponse
from django.conf import settings
from app01 import models
from app01.utils.excel_utils import get_data, get_classes_data, time_slot, write_excel

from django.db import transaction


# Create your views here.


def home(request):
    """
    首页
    :param request:
    :return:
    """

    return render(request, 'home.html', locals())


def push(request):
    """
    文件上传
    :param request:
    :return:
    """
    if request.method == 'GET':
        # print('>>>>')
        return render(request, 'push.html', locals())
    # 文件进行保存
    excel_file = request.FILES.get('excel_file')

    try:
        push_excel_obj = models.Push_Excel.objects.create(name=excel_file.name, push_excel=excel_file)
    except Exception as e:
        print(e, '>>>>')
        return redirect('pull')
    try:
        # 进行数据录入 excel  to mysql
        # file or directory: '/home/ubuntu/acip_/uploads\\push_excel\\完整.xlsx'

        # end_path = os.path.join(settings.BASE_DIR, r'uploads\push_excel\%s' % excel_file.name)
        # end_path = settings.BASE_DIR + r'\uploads\push_excel\%s' % excel_file.name
        end_path = os.path.join(settings.BASE_DIR, 'uploads', 'push_excel', excel_file.name)
        # 返回数据
        ret = get_data(end_path)
    except Exception as e:
        print(e)
        return redirect('pull')

    # 进行数据保存
    data_list = []
    for item in ret:
        excel_data_obj = models.Excel_data(name=push_excel_obj.name, push_excel=push_excel_obj, department=item[0],
                                           work_name=item[1], Attendance_number=item[2], Clock_time=item[3])
        data_list.append(excel_data_obj)
    models.Excel_data.objects.bulk_create(data_list)

    # 进行excel生成
    ret = models.Excel_data.objects.filter().values('work_name').annotate().values_list('work_name',
                                                                                        'Attendance_number',
                                                                                        'Clock_time')
    # 数据构造
    data_dic = get_classes_data(ret)
    end_data = time_slot(data_dic=data_dic)
    path = excel_file.name
    # file or directory: '/home/ubuntu/acip_/uploads\\push_excel\\完整.xlsx'

    # end_path = os.path.join(settings.BASE_DIR, r'uploads\pull_excel\%s' % path)
    # end_path = settings.BASE_DIR + r'\uploads\pull_excel\%s' % path
    end_path = os.path.join(settings.BASE_DIR, 'uploads', 'pull_excel', path)

    write_excel(end_data, path=end_path)
    # 记录生成
    try:
        models.After_Conversion.objects.create(pull_name=excel_file.name, pull_path=end_path)
    except Exception as e:
        print(e, '>>>>||>>>>')

        return redirect('pull')
    # 数据删除
    return redirect('pull')


def pull(request):
    """
    文件上传
    :param request:
    :return:
    """
    if request.method == 'GET':
        query = models.After_Conversion.objects.all().order_by('-id')

        return render(request, 'pull.html', locals())
    # 数据的导出


def download_file(request):
    """
    下载
    """
    id = request.GET.get('id')

    file_obj = models.After_Conversion.objects.filter(id=id).first()
    # http://127.0.0.1:8000/media/book/book.txt
    # http://127.0.0.1:8000/media/book/book.jpg  # 现在：book/book.txt
    # linux测试数据
    # url = 'http://127.0.0.1:8000' + '/media/pull_excel/' + file_obj.pull_name
    url = 'http://1.116.131.59:80' + '/media/pull_excel/' + file_obj.pull_name
    print(url)
    # url = 'http://1.116.131.59:80' + '/media/pull_excel/' + file_obj.pull_name
    # url = 'https://t33-1305448189.cos.ap-nanjing.myqcloud.com/p333.jpg'
    # print(url)
    # 大文件的下载
    # data = requests.get(url).iter_content()
    # 正常文件的下载
    data = requests.get(url).content
    # 弹框提示框
    response = HttpResponse(data, content_type="application/octet-stream")
    # 中文转义
    from django.utils.encoding import escape_uri_path
    # 设置下载头
    # print(file_obj.my_file.url)
    # name = file_obj.my_file.url.split('/')[-1]
    # print(name)
    response['Content-Disposition'] = "attachment;filename={};".format(escape_uri_path(file_obj.pull_name))
    return response

#
# def text(request):
#     """
#     测试
#     :param request:
#     :return:
#     """
#     """
#
#
#     """
#
#     ret = models.Excel_data.objects.filter().values('work_name').annotate().values_list('work_name',
#                                                                                         'Attendance_number',
#                                                                                         'Clock_time')
#     # for item in ret:
#     #     print(item)
#     # print(ret)
#     # 数据构造
#     data_dic = get_classes_data(ret)
#     end_data = time_slot(data_dic=data_dic)
#     path = '666.xlsx'
#     end_path = os.path.join(settings.BASE_DIR, f'uploads\pull_excel\{path}')
#     write_excel(end_data, path=end_path)
#     return HttpResponse('ok')
